#!/bin/bash -e

# install.sh -- install the applicacion

# Copyright (C) 2011 Friedrich Gonzalez
#
#   This file is part of multitomcat
#
#   multitomcat is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   multitomcat is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
#


Error[0]="Unknown Error"
Error[1]="Install directory already exists, remove this directory or choose another"
Error[2]="I can't create the install directory"
Error[3]="I can't file needed files from the packages, re-download the package"
Error[4]="I can't copy the files'"
Error[10]="You need to install Sun JDK 1.6 first"
Error[11]="It doesn't seem to be a valid JDK"

function quitError {
	echo "Error: ${Error[$1]}"
	exit
}

echo "------------------------------------"
echo "MultiTomcat Installation Script 0.01"
echo "------------------------------------"

[[ ! -z "$JAVA_HOME" ]] && hint="(Hit Enter if you think is $JAVA_HOME)"
echo "Enter your JAVA_HOME directory $hint"
echo -n " >"
read java_home
if [[ -z "$java_home" ]]; then
 if [[ -z "$JAVA_HOME" ]]; then
	quitError 10
 else
	java_home=$JAVA_HOME
 fi
fi
[[ ! -x $java_home/bin/java ]] && quitError 11
export JAVA_HOME=$java_home
export PATH=$JAVA_HOME/bin:$PATH

echo "Using JAVA_HOME=$JAVA_HOME"
echo
echo "Enter your installation directory"
echo -n " (Default: /opt/multitomcat) >"
read installdir

[[ -z "$installdir" ]] && installdir="/opt/multitomcat"

# If the installdir/bin directory exists we assume there is another installation of multitomcat there
[[ -e "${installdir}" ]] && [[ -e "${installdir}/bin" ]] && quitError 1

if [[ ! -e "$installdir" ]]; then

 # If we don't have permission to write to basedirectory
 if [[ ! -w "$(dirname $installdir)" ]]; then
   if [[ -x "/usr/bin/sudo" ]]; then
        echo "Looks like you can use sudo ..."
	set -x
        ! /usr/bin/sudo /bin/mkdir $installdir && quitError 2
        ! /usr/bin/sudo /bin/chown $USER $installdir && quitError 2
	set +x
   elif [[ -x "/bin/su" ]]; then
        echo "Looks like you can use su ..."
        echo "Enter your root to execute these command: mkdir $installdir && chown $USER $installdir"
	set -x
        ! /bin/su -c "mkdir $installdir" && quitError 2
        ! /bin/su -c "chown $USER $installdir" && quitError 2
	set +x
   else
        echo "Execute the following command as root to continue"
        echo "mkdir $installdir"
        echo "chown $USER $installdir"
        quitError 2
   fi
 else
   # If we do have permission to write to base directory
   mkdir $installdir
 fi
fi

[[ ! -w "$installdir" ]] && quitError 4

# Creating directories
echo "Starting installation"
! mkdir -p $installdir/bin 2>/dev/null && quitError 2
! mkdir -p $installdir/instances 2>/dev/null && quitError 2
! mkdir -p $installdir/etc 2>/dev/null && quitError 2
! mkdir -p $installdir/version 2>/dev/null && quitError 2
! mkdir -p $installdir/download 2>/dev/null && quitError 2
[[ ! -e mtomcat.sh ]] && quitError 3
! cp mtomcat.sh $installdir/bin && quitError 4
! cp LICENSE $installdir && quitError 4
echo "Installation completed successfully"
